
var arr = [
	{city: "上海", location: "浦东"},
	{city: "上海", location: "静安"},
	{city: "北京", location: "内环"},
	{city: "北京", location: "五环"},
	{city: "苏州", location: "苏州"},
]

console.log( arrayGroup(arr, 'city') );


function arrayGroup(arr, property) {
	return arrayGroupV2.apply(null, arguments)
}


function search(arr, str) {
    var res = [];
    for(var i in arr) {
        if (arr[i].city == str) {
            res.push(arr[i]);
        }
    }
    return res;
}



function arrayGroupV2 (arr, prop) {
	var obj = {}
	arr.forEach(item => {
		var key = item[prop]
		if(obj[key]) {
			obj[key].push(item)
		} else {
			obj[key] = [item]
		}
	})

	return obj
}



function arrayGroupV1 (arr, prop) {
	return arr.reduce((prev, item) => {
		var key = item[prop]
		prev[key] =  prev[key] || []
		prev[key].push(item)
		return prev
	}, {})
}